﻿/*+===================================================================
File:      EnemyBulletBig.cpp

Summary:   Định nghĩa các phương thức của CEnemyBulletBig.
===================================================================+*/

#include "EnemyBulletBig.h"
#include "Explosion.h"
#include "Gun.h"
#include "PlayState.h"

CEnemyBulletBig::CEnemyBulletBig(D3DXVECTOR2 pos, int id, int typeID, float angle) : 
CBullet(pos, id, typeID, angle)
{
	m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S3_BULLET);

	m_v.x = -200;
	m_v.y = 200;
	m_a.y = -500;
}


CEnemyBulletBig::~CEnemyBulletBig()
{
}

void CEnemyBulletBig::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{

	float timeCollision = timeFrame;
	float normalx = 0.0f, normaly = 0.0f;
	CGameObject* objCollision = NULL; // Đối tượng va chạm sớm nhất.

	for (int i = 0; i < vecGameObjs.size(); i++)
	{
		if (vecGameObjs[i]->IsLive())
		{
			float timeCollision = CheckCollision(vecGameObjs[i], normalx, normaly, timeFrame);

			if (timeCollision < timeFrame)
			{
				if (vecGameObjs[i]->GetTypeId() == ID_GRASS && normaly == 1)
				{
					m_v += m_a * timeCollision;
					m_a.y = 0.0f;
					Kill();
				}
			}
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		this->Kill();
		objCollision->Kill();
	}

	// Update vị trí.
	m_v += m_a * timeFrame;
	m_pos += m_v * timeFrame;
}

void CEnemyBulletBig::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	D3DXVECTOR2 posInViewPort = CViewport::GetInstance()->TransformMatrix(m_pos);

	if (posInViewPort.x < 0 || posInViewPort.x > SCREEN_WIDTH || posInViewPort.y < 0 || posInViewPort.y > SCREEN_HEIGHT)
	{
		m_bIsLive = false;
		m_pContainerGun->ReloadBullet();
		return;
	}

	UpdateBox(timeFrame);
}

void CEnemyBulletBig::Draw()
{
	if (!m_bIsLive)
		return;
	D3DXVECTOR2 pos;
	pos = CViewport::GetInstance()->TransformMatrix(m_pos);
	
	m_pSprite->Draw(D3DXVECTOR2((INT)pos.x, (INT)pos.y));
}

bool CEnemyBulletBig::Kill()
{
	m_bIsLive = false;
	m_pContainerGun->ReloadBullet();

	CExplosion* explosion = new CExplosion(m_pos, 0, ID_SMALL_EXPLOSION);
	CPlayState::s_objsOutSideQuadTree.push_back(explosion);

	return true;
}